<?php
// 本类由系统自动生成，仅供测试用途
class ShopAction extends Action {
    public function index(){
        $taobao_cache = M("taobao_cache");
        $created = time()-3600*24*3;
        $Cache = $taobao_cache->field("*")->where("created < ".$created)->order("created asc")->select();        
        foreach ($Cache as $c ){
            $tid = $c['tcache_id'];
//             print_r($c);
            if($tid !=""){
//               $this->logCmd(date("Y-m-d H:i:s",$c['created']));
              $cmd = " /usr/local/php/bin/php -c /usr/local/php/etc/php_cli.ini index.php Index newcache tid ".$tid;
              passthru($cmd);
              sleep(1);
            }
        }
        $Cache = $taobao_cache->field("*")->order("created asc")->find();
        $this->logCmd("The Old  Time :".date("Y-m-d H:i:s",$Cache['created']));
        $this->logCmd("The Next Time :".date("Y-m-d H:i:s",$Cache['created']+3600*24*3));
        $this->logCmd("OK TaoBaoCache is News"."\n") ;
    }
    
    protected function delCache($tcache_id,$view,$code=0){
        $this->logCmd($tcache_id."==>".$view."==>".$code);
        $Goods = M("Goods");
        $GoodsData= array();
        $GoodsData['class_id']=0;
        $GoodsData['audit']=10;
        $Goods->where(array("tcache_id"=>$tcache_id,"ispairs"=>0))->save($GoodsData);
        $TaobaoCache = M ( "TaobaoCache" );
        $Cache['created'] =time()-1*24*3600;
        $Cache['approve_status']="instock";
        $TaobaoCache->where ( array ("tcache_id" => $tcache_id ) )->save($Cache);
//         $this->logCmd(date("Y-m-d H:i:s",time()-4*24*3600));
//         $this->logCmd($TaobaoCache->_sql());
    }
    
    protected function logCmd($view){
        echo "\n".date("Y-m-d H:i:s")." : ".$view."\n";
    }
    
    public function newcache(){
       require './top/TopClient.php'; 
       require './top/RequestCheckUtil.php';
       require './top/Logger.php';
       require './top/request/ItemGetRequest.php';       
       $CacheId = $_GET["tid"];     
       $TopClient = new TopClient ();
       $TopClient->appkey = "21087743";
       $TopClient->secretKey = "5f0cf1f8200d4854cb87410620878159";
       $ItemGetRequest = new ItemGetRequest ();
       $ItemGetRequest->setFields ( "detail_url,num_iid,title,nick,type,created,auction_point,cid ,input_str,pic_url,list_time,delist_time,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,modified,approve_status,product_id,score,one_station,second_kill,violation,is_prepay,wap_desc,wap_detail_url" );
       $ItemGetRequest->setNumIid ( $CacheId );
       $TBCache =  $TopClient->execute ( $ItemGetRequest ); 
       if(isset($TBCache->code)){
           $this->delCache($CacheId, "Goods have been canceled shopping",$TBCache->code);
           return false;
       }
       $ApiItem = $TBCache->item;
       if( (string) $ApiItem->approve_status =="instock"){
           $this->delCache($CacheId, "Commodities have been under the frame","instock");
           return false;
       }
       
       /**
        * 更新产品
        * 
        */
       
       $TaobaoCache = M ( "TaobaoCache" );
//        $this->logCmd($TaobaoCache->count());
       $Tb_Cache=$TaobaoCache->where(array ("tcache_id" => $CacheId ) )->find();
       $Cache =array();
       $Cache ['approve_status'] =(string) $ApiItem->approve_status; //是否进行了销售
       $Cache ['title'] = (string)$ApiItem->title; //标题
       $Cache ['nick'] =(string) $ApiItem->nick; //卖家昵称
       if($Tb_Cache['pic_url'] !=(string)$ApiItem->pic_url){
           import ( "ORG.Net.HttpFile" );
           $HttpFilees = HttpFile::sendlUrl ( (string)$ApiItem->pic_url, "pic" );
           $Cache ['host_pic'] = $HttpFilees->path;
       }else{
           $Cache ['host_pic'] = $Tb_Cache['host_pic'];
       }
       $Cache ['pic_url'] = (string)$ApiItem->pic_url;
       $Cache ['price'] = (string)$ApiItem->price; //销售价格
       $Cache ['click_url'] = (string)$ApiItem->detail_url; //商品地址
       $Cache ['shop_click_url'] = (string)$ApiItem->shop_click_url; //店铺地址
       $Cache ['seller_credit_score'] =(string) $ApiItem->seller_credit_score; //卖家信誉等级
       $Cache ['auction_point'] =(string) $ApiItem->auction_point; //返点比率
       $Cache ['cid'] =(string) $ApiItem->cid; //淘宝上级栏目
       $Cache ['delist_time'] = strtotime ( (string)$ApiItem->delist_time ); //下架时间
       $Cache ['wap_detail_url'] = (string)$ApiItem->wap_detail_url; //WAP 地址
       $Cache ['created'] = time (); //本次更新时间
       $TaobaoCache->where ( array ("tcache_id" => $CacheId ) )->save ( $Cache );
        
       /**
        * 更新Goods表的价格标题图片
        */
       
       $AutoNews =false;
       if($Cache ['title'] != $Tb_Cache['title']){
           $AutoNews = true;
       }
      
       if($Cache ['price'] != $Tb_Cache['price']){
           $AutoNews = true;
       }
       
       if($Cache ['pic_url'] != $Tb_Cache['pic_url']){
           $AutoNews = true;
       }
       if($AutoNews){
           $GoodsData= array();
           $GoodsData['title'] = $Cache ['title'];
           $GoodsData['price']=$Cache ['price'];
           $GoodsData['pic_url']=$Cache ['host_pic'];
           $GoodsData['class_id']=0;
           $GoodsData['audit']=10;
           $Goods = M("Goods");           
           $Goods->where(array("tcache_id"=>$CacheId,"ispairs"=>0))->save($GoodsData);
           $this->logCmd("AutoNews==True Update Goods : $CacheId");
        }
        $View = $Cache ['click_url'];
         if($Tb_Cache['pic_url'] !=(string)$ApiItem->pic_url){
             $View = $View."==>". $Cache ['host_pic'];
         }
       $this->logCmd($View);
       return true;
    }
}